-------------------------------------------------------------------------------
--	 File name: init.lua
--		Author: Sploder
-- 		  Date: 04/06/2010
-- Description: This file contains the main body of code used by Cirno's
--				ice columns.
--
-------------------------------------------------------------------------------

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

-------------------------------------------------------------------------------
--	  Function: ENT:Touch(hitent)
-- Description: This function is called when something is touching the entity.
-------------------------------------------------------------------------------
function ENT:Touch(hitent)
	-- If the entity is another column, or if the column is done rising, don't do anything.
	if (CurTime() > self.timer2 || hitent:GetClass() == "sent_proj_cirnoice03")
	then
		return
	end
	
	-- If the hit entity is not the owner, then throw do damage and throw it into the sky.
	if (hitent != self.Owner)
	then
		self:Hit(hitent)
	end
end

-------------------------------------------------------------------------------
--	  Function: ENT:Hit(hitent)
-- Description: This function is called to "hit" the provided entity, sending
--				it skyward and doing damage.
-------------------------------------------------------------------------------
function ENT:Hit(hitent)
	-- If, somehow, we don't have an owner, set this entity's owner to itself.
	if (!self.Owner || !self.Owner:IsValid())
	then
		self.Owner = self
	end
	
	-- If the hit entity is valid, damage and throw it into the sky!
	if (hitent && hitent:IsValid())
	then
		local dmg = DamageInfo()
		dmg:SetAttacker(self.Owner)
		dmg:SetInflictor(self)
		dmg:SetDamage(self.damage)
		dmg:SetDamageForce(vector_origin * 1)
		dmg:SetDamagePosition(self:GetPos())
		dmg:SetDamageType(DMG_GENERIC)
		hitent:TakeDamageInfo(dmg)
		
		-- If it's an NPC or player, throw it into the sky and count kills.
		if (hitent:IsNPC() || hitent:IsPlayer())
		then
			-- If the hit entity died from this shot, add a frag for it.
			if (hitent:IsNPC() && hitent:GetNPCState() == 7)
			then
				self.Owner:AddFrags(1)
			else
				hitent:SetVelocity(Vector(0,0,1) * self.strength / 2)
			end

		-- If it's not an NPC or player, just throw it into the sky.
		else
			local phys = hitent:GetPhysicsObject()
			if (phys:IsValid())
			then
				phys:ApplyForceCenter(Vector(0,0,1) * self.strength * 5)
			end
		end
	end
end

-------------------------------------------------------------------------------
--	  Function: ENT:Think()
-- Description: This function is called every step (or so) to do stuff...
-------------------------------------------------------------------------------
function ENT:Think()
	-- Set the next think to be the next frame.
	self:NextThink(CurTime())
	
	-- If the entity has been around past the kill timer, kill it.
	if (CurTime() > self.timer)
	then
		self:Remove()
		WorldSound("cirno/iceBreakLarge.wav", self:GetPos(), 60, 100)
		
		--[[ Make a hit effect.
		local edata = EffectData()
		edata:SetStart(self:GetPos())
		edata:SetOrigin(self:GetPos())
		edata:SetScale(1)
		util.Effect("sprite_knifeHit", edata)]]
	end
	
	-- If the entity has past timer2, stop its motion.
	if (self.timer2 != 0 && CurTime() > self.timer2)
	then
		self:SetMoveType(MOVETYPE_NONE)
		self.timer2 = 0
	end
	
	return true
end

-------------------------------------------------------------------------------
--	  Function: ShouldCollide Hook
-- Descrption: This function determines if various entities can collide or not.
-------------------------------------------------------------------------------
hook.Add("ShouldCollide", "ShouldCollideColumnHook",
	function(ent1, ent2)
		-- If both entities are columns, don't collide.
		if (ent1:GetClass() == "sent_proj_cirnoice03" && ent2:GetClass() == "sent_proj_cirnoice03")
		then
			return false
		-- If one entity is a player (or NPC) and the other is a column, and the player (or NPC) is flying in the sky, don't collide anymore.
		elseif ((ent1:IsPlayer() || ent1:IsNPC()) && (ent2:GetClass() == "sent_proj_cirnoice03"))
		then
			if (ent1:GetVelocity().z >= (ent2.strength / 4))
			then
				return false
			end
		elseif ((ent2:IsPlayer() || ent2:IsNPC()) && (ent1:GetClass() == "sent_proj_cirnoice03"))
		then
			if (ent2:GetVelocity().z >= (ent1.strength / 4))
			then
				return false
			end
		end
	end
)